﻿using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using DAL;
using BLL;
using System.Text;


public partial class Plublic_BankTransfer : System.Web.UI.Page
{
    String StrConn = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    CardsHandler objCardsHandler = new CardsHandler();
    NonTermCurrentHandler objNonTermCurrentHandler = new NonTermCurrentHandler();
    TermCurrentHandler objTermCurrentHandler = new TermCurrentHandler();
    TransactionHandler objTransactionHandler = new TransactionHandler();
    TransactionCategoriesHandler objTransactionCategoriesHandler = new TransactionCategoriesHandler();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"]==null)
        {
            Response.Redirect("Default.aspx");
        }
        if (!IsPostBack)
        {

            this.BindDropDowListAccount();
            this.BindDropDowListTypeTransfer();

        }
        
    }

    protected void BindDropDowListTypeTransfer()
    {
        List<TransactionCategories> lstTransactionCategories = new List<TransactionCategories>();

        lstTransactionCategories = objTransactionCategoriesHandler.GetTransactionCategoriesList(StrConn);

        ddlTransactionCategories.DataSource = lstTransactionCategories;
        ddlTransactionCategories.DataValueField = "TransactionCategoriesID";
        ddlTransactionCategories.DataTextField = "Name";
        ddlTransactionCategories.DataBind();
    }
    protected void BindDropDowListAccount() 
    {
        int UserId = Convert.ToInt32(Session["UserId"]);
        Cards objCards = new Cards();

        DataTable dt =  objCardsHandler.GetCardsTable(UserId, StrConn);

        ddlTranferAccount.DataSource = dt;
        ddlTranferAccount.DataValueField = "AccountNumber";
        ddlTranferAccount.DataBind();

        

    }
    

    protected void btnGetInfo_Click(object sender, System.EventArgs e)
    {
        string AccountReceived = txtReceivedAccount.Text.ToString();
        string AccountTransfer = ddlTranferAccount.Text.ToString();

        if (AccountReceived.Equals(AccountTransfer))
        {
            lblErrorCoincide.Text = "You can't transfer funds between the same account!!!";
        }
        else
        {
            DataTable dt = objCardsHandler.GetInfoReceived(AccountReceived, StrConn);

            if (dt.Rows.Count == 1)
            {
                DataRow row = dt.Rows[0];

                lblErrorCoincide.Text = "";
                txtNameReceived.Text = row["AccountHolders"].ToString();
                txtCurrencyReceived.Text = row["Currency"].ToString();

            }
            else
            {
                txtNameReceived.Text = "";
                txtCurrencyReceived.Text = "";    
            }
        }
        
    }
    protected void btnTransfer_Click(object sender, System.EventArgs e)
    {
        string AccTransfer = ddlTranferAccount.SelectedItem.ToString();
        string AccReceived = txtReceivedAccount.Text.ToString();
        double AmountTransfer = Convert.ToDouble(txtAmountTransfer.Text);
        double BalanceTransfer = Convert.ToDouble(txtBalanceTransfer.Text);
        Transaction objTransaction = new Transaction();
        TermCurrent objTermCurrent = new TermCurrent();

        objTransaction.Created = DateTime.Now.ToString();
        objTransaction.Updated = DateTime.Now.ToString();
        objTransaction.AccountNumber = AccTransfer;
        objTransaction.AccountNumberTo =AccReceived;
        objTransaction.TransactionCategoriesID = Convert.ToInt32(ddlTransactionCategories.SelectedValue);
        objTransaction.DateTrans = DateTime.Now.ToString(); ;
        objTransaction.Balance = BalanceTransfer;
        objTransaction.AmountTransaction = AmountTransfer;
        objTransaction.Currency = txtCurrencyTransfer.Text.ToString();
        objTransaction.UserID = Convert.ToInt32(Session["UserId"]);
        objTransaction.Descriptions = txtDescription.Text.ToString();
        objTransaction.Status = "1";
        objTransaction.Acction = "Transfer";
        objTransaction.ErrorMessage = "";

        objTermCurrent = objTermCurrentHandler.GetMaturityTermCurrent(AccReceived, StrConn);
        if (AmountTransfer < BalanceTransfer)
        {

            if (objTermCurrent != null)
            {
                DateTime maTuRiTy = Convert.ToDateTime(objTermCurrent.MaturityDate);

                if (maTuRiTy > DateTime.Now)
                {
                    lblErrorTermCurrent.Text = "The account is term current account because the day is not maturity date!!!";
                }
                else
                {
                    objTermCurrentHandler.UpdateTermCurrentBalanceReceived(AccReceived, AmountTransfer, StrConn);
                    objCardsHandler.UpdateBalanceTransfer(AccTransfer, AmountTransfer, StrConn);
                    objTransactionHandler.AddNewTransaction(objTransaction, StrConn);
                    this.setText();
                    lblErrorTermCurrent.Text = "";
                }
            }
            else
            {

                objNonTermCurrentHandler.UpdateNonTermBalanceReceived(AccReceived, AmountTransfer, StrConn);
                objCardsHandler.UpdateBalanceReceived(AccReceived, AmountTransfer, StrConn);
                objCardsHandler.UpdateBalanceTransfer(AccTransfer, AmountTransfer, StrConn);
                objTransactionHandler.AddNewTransaction(objTransaction, StrConn);
                this.setText();
            }
        }
        else
        {
            lblErrorAmount.Text = "Please,check amount transfer";
        }
        
        
    }
    protected void setText()
    {
        double AmountTransfer = Convert.ToDouble(txtAmountTransfer.Text);
        double BalanceTransfer = Convert.ToDouble(txtBalanceTransfer.Text);

        txtReceivedAccount.Text = "";
        txtAmountTransfer.Text = "";
        txtCurrencyReceived.Text = "";
        txtNameReceived.Text = "";
        txtDescription.Text = "";

        txtBalanceTransfer.Text = (BalanceTransfer - AmountTransfer).ToString(); ;

    }

    protected void ddlTranferAccount_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        String TransferAcc = ddlTranferAccount.SelectedItem.ToString();

        if (TransferAcc != string.Empty)
        {
            Cards objCards = new Cards();
            objCards = objCardsHandler.GetCardsDetailsTransfer(TransferAcc, StrConn);

            if (objCards != null)
            {
                txtBalanceTransfer.Text = objCards.Balance.ToString();

                txtCurrencyTransfer.Text = objCards.Currency.ToString();
            }

        }
    }
    protected void ddlTranferAccount_Load(object sender, System.EventArgs e)
    {
        String TransferAcc = ddlTranferAccount.Text.ToString();

        if (TransferAcc != string.Empty)
        {
            Cards objCards = new Cards();
            objCards = objCardsHandler.GetCardsDetailsTransfer(TransferAcc, StrConn);

            if (objCards != null)
            {
                txtBalanceTransfer.Text = objCards.Balance.ToString();

                txtCurrencyTransfer.Text = objCards.Currency.ToString();
            }

        }
    }
    
}